//
//  SFTagView.m
//  WrapViewWithAutolayout
//
//  Created by shiweifu on 12/9/14.
//  Copyright (c) 2014 shiweifu. All rights reserved.
//

#import "SFTagView.h"
#import "SFTag.h"
#import "SFTagButton.h"

@interface SFTagView ()

@property (nonatomic, strong) NSMutableArray *tags;
@property (assign) CGFloat intrinsicHeight;

@end

@implementation SFTagView
{
}

-(CGSize)intrinsicContentSize {
  return CGSizeMake(self.frame.size.width, self.intrinsicHeight);
}

- (void)addTag:(SFTag *)tag
{
  SFTagButton *btn = [[SFTagButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
  [btn setTitle:tag.text forState:UIControlStateNormal];
  [btn.titleLabel setFont:tag.font];
  [btn setBackgroundColor:tag.bgColor];
  [btn setTitleColor:tag.textColor forState:UIControlStateNormal];
  [btn addTarget:tag.target action:tag.action forControlEvents:UIControlEventTouchUpInside];
    btn.tag = tag.tagIndex;

    
    CGSize size;
    CGSize constraintSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
#ifdef __IPHONE_7_0
    NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraphStyle setAlignment:NSTextAlignmentLeft];
    [paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
    
    NSDictionary* stringAttributes = @{NSFontAttributeName: tag.font,
                                       NSParagraphStyleAttributeName: paragraphStyle};
    size = [tag.text boundingRectWithSize: constraintSize
                                  options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                               attributes: stringAttributes
                                  context: nil].size;
#else
    size = [tag.text sizeWithFont: tag.font
                constrainedToSize: constraintSize
                    lineBreakMode: NSLineBreakByCharWrapping];
#endif
    
  CGFloat i = tag.leftRightMargin;
  CGFloat j = tag.topBottomMargin;
  if(i == 0){
        i = 5;
    }
    if(j == 0){
        j = 5;
    }
  
  size.width  += i * 2;
  size.height += j * 2;

  btn.layer.cornerRadius = tag.cornerRadius;
  btn.layer.borderWidth = tag.borderWidth;
  btn.layer.borderColor = tag.borderColor.CGColor;
  [btn.layer setMasksToBounds:YES];

//  CGSize size = btn.intrinsicContentSize;
  CGRect r = CGRectMake(0, 0, size.width, size.height);
  [btn setFrame:r];

  [self.tags addObject:btn];

  [self rearrangeTags];
}

#pragma mark - Tag removal

- (void)removeTagText:(NSString *)text
{
  SFTagButton *b = nil;
  for (SFTagButton *t in self.tags) {
    if([text isEqualToString:t.titleLabel.text])
    {
      b = t;
    }
  }

  if(!b)
  {
    return;
  }

  [b removeFromSuperview];
  [self.tags removeObject:b];
  [self rearrangeTags];
}
-(void)setupOneSelectedTag:(NSInteger)index
{
    for (int i = 0; i<self.tags.count; i++) {
        SFTagButton *t = (SFTagButton *)self.tags[i];
        if (i == index) {
            [t setTitleColor:MainColor forState:UIControlStateNormal];
            t.layer.borderColor = MainColor.CGColor;
            self.selectedTagText = t.titleLabel.text;
            self.selectedTagindex = index+1; //在此处多加1，是为了区别未选中时默认也为0
        }else{
            [t setTitleColor:SetColorRGBValue(0x333333, 1.0) forState:UIControlStateNormal];
            t.layer.borderColor = SetColorRGBValue(0xe0e0e0, 1.0).CGColor;
        }
    }
}
- (void)removeAllTags
{
  for (SFTagButton *t in self.tags) {
    [t removeFromSuperview];
  }
  [self.tags removeAllObjects];
  [self rearrangeTags];
}

- (void)rearrangeTags
{
    // 左对齐布局
    if (self.alignment == 0)
    {
        [self.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
            [obj removeFromSuperview];
        }];
        __block float maxY = self.margin.top;
        __block float maxX = self.margin.left;
        __block CGSize size;
        [self.tags enumerateObjectsUsingBlock:^(SFTagButton *obj, NSUInteger idx, BOOL *stop) {
            size = obj.frame.size;
            [self.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
                if ([obj isKindOfClass:[SFTagButton class]]) {
                    maxY = MAX(maxY, obj.frame.origin.y);
                }
            }];
            
            [self.subviews enumerateObjectsUsingBlock:^(SFTagButton *obj, NSUInteger idx, BOOL *stop) {
                if ([obj isKindOfClass:[SFTagButton class]]) {
                    if (obj.frame.origin.y == maxY) {
                        maxX = MAX(maxX, obj.frame.origin.x + obj.frame.size.width);
                    }
                }
            }];
            
            // Go to a new line if the tag won't fit
            if (size.width + maxX + self.insets > (self.frame.size.width - self.margin.right)) {
                maxY += size.height + self.lineSpace;
                maxX = self.margin.left;
            }
            obj.frame = (CGRect){maxX + self.insets, maxY, size.width, size.height};
            [self addSubview:obj];
        }];
        
        CGRect r = self.frame;
        CGFloat n = maxY + size.height + self.margin.bottom;
        self.intrinsicHeight = n > self.intrinsicHeight? n : self.intrinsicHeight;
        [self setFrame:CGRectMake(r.origin.x, r.origin.y, self.frame.size.width, self.intrinsicHeight)];
    }
    // 右对齐布局
    else
    {
        [self.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
            [obj removeFromSuperview];
        }];
        __block float maxY = self.margin.top;
        __block float maxX = self.margin.left;
        __block CGSize size;
        [self.tags enumerateObjectsUsingBlock:^(SFTagButton *obj, NSUInteger idx, BOOL *stop) {
            size = obj.frame.size;
            [self.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
                if ([obj isKindOfClass:[SFTagButton class]]) {
                    maxY = MAX(maxY, obj.frame.origin.y);
                }
            }];
            
            [self.subviews enumerateObjectsUsingBlock:^(SFTagButton *obj, NSUInteger idx, BOOL *stop) {
                if ([obj isKindOfClass:[SFTagButton class]]) {
                    if (obj.frame.origin.y == maxY) {
                        maxX = MAX(maxX, self.frame.size.width-obj.frame.origin.x);
                    }
                }
            }];
            
            // Go to a new line if the tag won't fit
            if (size.width + maxX + self.insets > (self.frame.size.width - self.margin.right)) {
                maxY += size.height + self.lineSpace;
                maxX = self.margin.left;
            }
            obj.frame = (CGRect){self.frame.size.width - obj.frame.size.width - maxX - self.insets, maxY, size.width, size.height};
            [self addSubview:obj];
            NSLog(@"%@---%@",@(self.frame.size.width - obj.frame.size.width - maxX - self.insets),@(maxX));
        }];
        
        CGRect r = self.frame;
        CGFloat n = maxY + size.height + self.margin.bottom;
        self.intrinsicHeight = n > self.intrinsicHeight? n : self.intrinsicHeight;
        [self setFrame:CGRectMake(r.origin.x, r.origin.y, self.frame.size.width, self.intrinsicHeight)];
    }
}
- (void)layoutSubviews
{
  [super layoutSubviews];
  [self rearrangeTags];
}

- (NSMutableArray *)tags
{
  if(!_tags)
  {
    _tags = [NSMutableArray array];
  }
  return _tags;
}

@end
